/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.projects.tests.uml2model_test.business.dao.impl;

import java.util.List;

import com.genia.toolbox.model.exemple.B;
import com.genia.toolbox.model.exemple.impl.AssociateOfBImpl;
import com.genia.toolbox.model.exemple.impl.BImpl;
import com.genia.toolbox.persistence.criteria.Criteria;
import com.genia.toolbox.persistence.criteria.CriteriaResult;
import com.genia.toolbox.persistence.criteria.CriteriaType;
import com.genia.toolbox.persistence.dao.AbstractDAO;
import com.genia.toolbox.persistence.exception.PersistenceException;
import com.genia.toolbox.projects.tests.uml2model_test.business.dao.BbusinessDao;

/**
 * this class is the implementation of the <code>BbusinessDao</code>.
 */
public class BbusinessDaoImpl
    extends AbstractDAO
    implements BbusinessDao
{

  /**
   * this method create a new instance of type <code>BImpl</code>.
   * 
   * @return the object of type <code>BImpl</code>.
   */
  public BImpl createNewB()
  {
    return new BImpl();
  }



  /**
   * this methode delete an object <code>B</code> from his identifier.
   * 
   * @param identifier
   *          the identifier associated to an object <code>BImpl</code>.
   * @throws PersistenceException
   *           occurs during persistence process.
   */
  public void delete(Long identifier)
      throws PersistenceException
  {
    delete(get(BImpl.class, identifier));
  }



  /**
   * delete all objects of type <code>B</code>.
   * 
   * @throws PersistenceException
   *           occurs during persistence process.
   */
  public void deleteAll()
      throws PersistenceException
  {
    for (B b : getListBs()) {
      delete(b.getIdentifier());
    }
  }



  /**
   * get an object <code>B</code> from his identifier.
   * 
   * @param identifier
   *          the identifier associated to an object <code>BImpl</code>.
   * @return the object of type <code>BImpl</code>.
   */
  public BImpl get(Long identifier)
  {
    return get(BImpl.class, identifier);
  }



  /**
   * get a list of objects <code>B</code>.
   * 
   * @return a list of all object of type <code>BImpl</code>.
   * @throws PersistenceException
   *           occurs during persistence process.
   */
  public List<BImpl> getListBs()
      throws PersistenceException
  {
    return simpleFind(getCriteria(BImpl.class));
  }



  /**
   * this methode save an object <code>B</code>.
   * 
   * @param objTestB
   *          is of type <code>B</code>.
   * @return the identifier associated to <code>B</code>.
   * @throws PersistenceException
   *           occurs during persistence process.
   */
  public Long saveB(B objTestB)
      throws PersistenceException
  {
    BImpl newObjB = new BImpl();

    newObjB.setBar(objTestB.getBar());
    newObjB.setBarFoo(objTestB.getBarFoo());
    newObjB.setFoo(objTestB.getFoo());
    newObjB.setFoobar(objTestB.getFoobar());
    newObjB.setTestEnum(objTestB.getTestEnum());
    newObjB.setTestValueInteger(objTestB.getTestValueInteger());
    newObjB.setTestValueString(objTestB.getTestValueString());
    newObjB.setTestValueText(objTestB.getTestValueText());
    newObjB.setDescB(objTestB.getDescB());
    newObjB.setOtherDesc(objTestB.getOtherDesc());

    save(newObjB);

    return newObjB.getIdentifier();
  }



  /**
   * this method update <code>BImpl</code>.
   * 
   * @param objTestB
   *          is of type <code>BImpl</code>.
   * @throws PersistenceException
   *           occurs during persistence process.
   */
  public void updateB(BImpl objTestB)
      throws PersistenceException
  {
    if (objTestB != null) {
      update(objTestB);
    }
  }



  /**
   * get the associate from <code>B</code>.
   * 
   * @param objTestB
   *          the object from which we get associate.
   * @return the object associate of type <code>AssociateOfBImpl</code>
   * @throws PersistenceException
   *           occurs during persistence process.
   */
  public AssociateOfBImpl getAssociate(B objTestB)
      throws PersistenceException
  {

    Criteria<BImpl> criteria = getCriteria(BImpl.class);
    CriteriaType<AssociateOfBImpl> criteriaType = criteria.join(AssociateOfBImpl.class);

    criteria.addRestriction(equals(getPropertyField(B.NAME_IDENTIFIER), getConstantField(objTestB.getIdentifier())));
    criteria.addRestriction(isIn(getObjectField(criteriaType), elements(getPropertyField(BImpl.NAME_ASSOCIATE))));

    List<CriteriaResult<BImpl>> criteriaRes = find(criteria);

    CriteriaResult<BImpl> res = criteriaRes.get(0);

    AssociateOfBImpl associate = res.get(criteriaType);

    return associate;
  }



  /**
   * add an associate of type <code>AssociateOfB</code> to bImpl.
   * 
   * @param bImpl
   *          the object from which we add an associate.
   * @param associateOfB
   *          associate of type <code>AssociateOfBImpl</code>
   * @throws PersistenceException
   *           occurs during persistence process.
   */
  public void addAssociateOfB(BImpl bImpl, AssociateOfBImpl associateOfB)
      throws PersistenceException
  {
    bImpl.addAssociate(associateOfB);
    update(bImpl);
  }



  /**
   * remove association of type <code>AssociateOfBImpl</code> to bImpl.
   * 
   * @param bImpl
   *          is of type <code>BImpl</code>.
   * @param associateOfB
   *          of type <code>AssociateOfBImpl</code>.
   * @throws PersistenceException
   *           occurs during persistence process.
   */
  public void removeAssociateOfB(BImpl bImpl, AssociateOfBImpl associateOfB)
      throws PersistenceException
  {
    bImpl.removeAssociate(associateOfB);
    updateB(bImpl);
  }

}
